<?php
require_once dirname(__FILE__) . '/../common/common.inc.php';
require_once dirname(__FILE__) . "/../database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

$table_user = 'pz_user';
$table_task = 'pz_task';

$action = MiscUtils::getParam('action', NULL);
$myManager->start_t();
try {
  switch ($action) {
    case 'searchtasks': 
      searchtasks();
      break;
    case 'findByRequest':
      findByRequest();
      break;
    case 'pz_task_note': 
      pz_task_note();
      break;
    case 'pz_task_event': 
      pz_task_event();
      break;
    case 'pz_task_track': 
      pz_task_track();
      break;
    case 'pz_user': 
      pz_user();
      break;
    case 'lastupdated':
      lastupdated();
      break;
    case 'getnotes':
      getnotes();
      break;
    case 'getevents':
      getevents();
      break;
    case 'prearrange':
      prearrange();
      break;
    case 'searchscoresheet':
      searchscoresheet();
      break;
    case 'getCouriers':
      getCouriers();
      break;
    case 'getCultures':
      getCultures();
      break;
    case 'getCeremonys':
      getCeremonys();
      break;
    case 'getReceptions':
      getReceptions();
      break;
    case 'getColors':
      getColors();
      break;
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}
$myManager->commit_t();

function searchtasks() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = json_decode(MiscUtils::getParam('c', NULL));
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', SIZE);
  
  
  $table_select = json_decode(MiscUtils::getParam('ts', '*'));
  $table_join = json_decode(MiscUtils::getParam('tj', NULL));
  
  $data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if ($order == 'null') {
      $order = NULL;
    }
    if (!$queue == 'null') {
      $queue = NULL;
    }
    $data['0'] = MiscUtils::db($myPdo, $table_select, 'pz_task', $table_join, $condition, $order, $queue, $page, $size);
    echo json_encode($data['0']);
    
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}


function findByRequest() {
  global $table_task, $table_user;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = MiscUtils::getParam('sql', '');
  if ($sql) {
    try {
      $host = DB_HOST;
      $login = DB_LOGIN;
      $pass = DB_PASS;
      $db = DB_NAME;
      $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
      $myPdo->exec('USE ' . $db);
      $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $i = 0; $count = 0;
      while ($i < $stmt->rowCount()) {
        $tmp = $stmt->fetch(PDO::FETCH_OBJ);
        $result->data[] = $tmp;
        $i++;
      }
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function pz_task_note() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = new stdClass();
  $result->data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $table_select = "t.*, 
                 t1.name AS user";
 
    $table_join = array(array('pz_user', 't1.id = t.note_user_id')
                       );
    
    $result->data = MiscUtils::db($myPdo, $table_select, 'pz_task_note', $table_join, $condition, $order, $queue, $page, $size)->data;
    echo json_encode($result);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function pz_task_event() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.id');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = new stdClass();
  $result->data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $table_select = "t.*, 
                 t1.name AS user";
 
    $table_join = array(array('pz_user', 't1.id = t.user_id')
                       );
    
    $result->data = MiscUtils::db($myPdo, $table_select, 'pz_task_event', $table_join, $condition, $order, $queue, $page, $size)->data;
    echo json_encode($result);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}


function pz_task_track() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = new stdClass();
  $result->data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $table_select = "t.*, 
                 t1.name AS photographer,
                 t2.name AS designer";
 
    $table_join = array(array('pz_user', 't1.id = t.photographer_id'),
                        array('pz_user', 't2.id = t.designer_id')
                       );
    
    $result->data = MiscUtils::db($myPdo, $table_select, 'pz_task_track', $table_join, $condition, $order, $queue, $page, $size)->data;
    echo json_encode($result);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}


function pz_user() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $data['0'] = MiscUtils::db($myPdo, 't.type', 'pz_user', NULL, $condition, $order, $queue, $page, $size);
    echo json_encode($data['0']);
    
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function lastupdated() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.id');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);

  $data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $select = "t.date, t.state_id as s_id, t.transition_id as t_id, t.message, t.overdue,
               t1.*,
               t2.name AS photographer, 
               t3.name AS designer,
               t4.name AS user,
               t5.name AS custom_assignee, 
               t6.name AS custom_assigner,
               t7.name AS photographer2";
    
    $join = array(array('pz_task', 't.task_id = t1.id'),
                array('pz_user', 't1.photographer_id = t2.id'), 
                array('pz_user', 't1.designer_id = t3.id'),
                array('pz_user', 't.user_id = t4.id'),
                array('pz_user', 't1.custom_assignee_id = t5.id'),
                array('pz_user', 't1.custom_assigner_id = t6.id'),
                array('pz_user', 't1.photographer2_id = t7.id')
               );

    $data['0'] = MiscUtils::db($myPdo, $select, 'pz_task_event', $join, $condition, $order, $queue, $page, $size);
    echo json_encode($data['0']);
    
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function getnotes() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = json_decode(MiscUtils::getParam('c', NULL));
  $order = MiscUtils::getParam('o', 't.id');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 99999);

  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    if ($condition == NULL) {
      $condition = '1=1';
    }
  
    $sql = "SELECT t.id FROM pz_task AS t WHERE $condition";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    $result = array();
    $i = 0;
    while ($i < $stmt->rowCount()) {
      $tmp = $stmt->fetch(PDO::FETCH_OBJ);
      $sql2 = "SELECT COUNT(n.id) AS total FROM pz_task_note AS n WHERE n.task_id = $tmp->id AND n.note_is_canceled = 0";
      $stmt2 = $myPdo->prepare($sql2);
      $stmt2->execute();
      $tmp->value = 0;
      if ($stmt2->rowCount() == 1) {
        $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
        $tmp->value = $tmp2->total;
      }
      $result[] = $tmp;
      $i++;
    }
    echo json_encode($result);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}


function getevents() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = json_decode(MiscUtils::getParam('c', NULL));
  $order = MiscUtils::getParam('o', 't.id');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', SIZE);
  
  
  $table_select = json_decode(MiscUtils::getParam('ts', '*'));
  $table_join = json_decode(MiscUtils::getParam('tj', NULL));
  
  $data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if ($order == 'null') {
      $order = NULL;
    }
    if (!$queue == 'null') {
      $queue = NULL;
    }
    $data['0'] = MiscUtils::db($myPdo, $table_select, 'pz_task_event', $table_join, $condition, $order, $queue, $page, $size);
    echo json_encode($data['0']);
    
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function prearrange() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.prearrange_date');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = new stdClass();
  $result->data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $table_select = "t.customer_id, t.prearrange_date, t.branch";
  
    
    $result->data = MiscUtils::db($myPdo, $table_select, 'pz_task', NULL, $condition, $order, $queue, $page, $size)->data;
    echo json_encode($result);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function searchscoresheet() {
$function = MiscUtils::getParam('f', NULL);
  $condition = json_decode(MiscUtils::getParam('c', NULL));
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  
  
  $table_select = json_decode(MiscUtils::getParam('ts', '*'));
  $table_join = json_decode(MiscUtils::getParam('tj', NULL));
  
  $data = array();
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if ($order == 'null') {
      $order = NULL;
    }
    if (!$queue == 'null') {
      $queue = NULL;
    }
    $data['0'] = MiscUtils::db($myPdo, $table_select, 'pz_task', $table_join, $condition, $order, $queue, $page, $size);
    $photographers = array();
    for ($i = 0, $il = count($data['0']->data); $i < $il; $i++) {
     $task = $data['0']->data[$i];
     $exist1 = false; $exist2 = false; $count1 = 1; $count2 = 1; $c1 = 0; $c2 = 0;
     for ($j = 0, $jl = count($photographers); $j < $jl; $j++) {
       $tmp = $photographers[$j];
       if ($tmp->name == $task->photographer) {
         $tmp->score += $task->score;
         $tmp->count++;
         if ($task->is_admin_retouched == 1) {
           $tmp->scount++;
         }
         $exist1 = true;
       }
       if ($tmp->name == $task->photographer2) {
         $tmp->score += $task->score;
         $tmp->count++;
         if ($task->is_admin_retouched == 1) {
           $tmp->scount++;
         }
         $exist2 = true;
       }
     }
    if (!$exist1 && $task->photographer != '') {
      $m = new stdClass();
      $m->name = $task->photographer;
      $m->score += $task->score;
      $m->count = $count1;
      if ($task->is_admin_retouched == 1) {
        $c1++;
      }
      $m->scount += $c1;
      array_push($photographers, $m);
    }
    if (!$exist2 && $task->photographer2 != '') {
      $m1 = new stdClass();
      $m1->name = $task->photographer2;
      $m1->score += $task->score;
      $m1->count = $count2;
      if ($task->is_admin_retouched == 1) {
        $c2++;
      }
      $m1->scount += $c2;
      array_push($photographers, $m1);
    }
   }
     echo json_encode($photographers);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}


function getCouriers() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.name');
  $queue = MiscUtils::getParam('q', 'ASC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = array();
  
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $data = MiscUtils::db($myPdo, '*', 'pz_courier', NULL, NULL, $order, $queue, $page, $size)->data;
    echo json_encode($data);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}


function getCultures() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.name');
  $queue = MiscUtils::getParam('q', 'ASC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = array();
  
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $data = MiscUtils::db($myPdo, '*', 'pz_culture', NULL, NULL, $order, $queue, $page, $size)->data;
    echo json_encode($data);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function getCeremonys() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.name');
  $queue = MiscUtils::getParam('q', 'ASC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = array();
  
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $data = MiscUtils::db($myPdo, '*', 'pz_ceremony', NULL, NULL, $order, $queue, $page, $size)->data;
    echo json_encode($data);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function getReceptions() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.name');
  $queue = MiscUtils::getParam('q', 'ASC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = array();
  
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $data = MiscUtils::db($myPdo, '*', 'pz_reception', NULL, NULL, $order, $queue, $page, $size)->data;
    echo json_encode($data);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}

function getColors() {
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 't.name');
  $queue = MiscUtils::getParam('q', 'ASC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);

  $result = array();
  
  try {
    $host = DB_HOST;
    $login = DB_LOGIN;
    $pass = DB_PASS;
    $db = DB_NAME;
    $myPdo = new PDO ('mysql:host=' . $host, $login, $pass);
    $myPdo->exec('USE ' . $db);
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $data = MiscUtils::db($myPdo, '*', 'pz_color', NULL, NULL, $order, $queue, $page, $size)->data;
    echo json_encode($data);
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
}
?>